package com.wg.msshopsp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.msshopsp.entity.*;
import com.wg.msshopsp.mapper.ShopMapper;
import com.wg.msshopsp.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopService extends ServiceImpl<ShopMapper, Shop> {
    @Autowired
    private ShopMapper shopMapper;
    //查询店铺下的所有商品
    public List<ProductVO> selectProductsByShopId(Integer id) {
        return shopMapper.selectProductsByShopId(id);
    }

    //查询所有店铺1(管理员)
    public Page<Shop> queryShops(Shop shop, QueryPageDto pager) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtil.isNotEmpty(shop.getShopName()), "shop_name", shop.getShopName())
                .eq(StringUtil.isNotEmpty(shop.getStatus()), "status", shop.getStatus())
                .like(StringUtil.isNotEmpty(shop.getCreateTime()), "create_time", shop.getCreateTime())
                .like(StringUtil.isNotEmpty(shop.getShopDesc()), "shop_desc", shop.getShopDesc());
        Page<Shop> pageInfo = shopMapper.selectPage(new Page<Shop>(pager.getPageNum(), pager.getPageSize()), queryWrapper);
        // 返回分页信息
        return pageInfo;
    }

    //查询所有店铺带用户信息(管理员)
    public IPage<ShopUserDTO> getShopWithUserPage(Integer pageNum, Integer pageSize) {
        // 处理默认参数（避免传入null）
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        // 创建分页对象，调用Mapper的分页查询方法
        IPage<ShopUserDTO> page = new Page<>(pageNum, pageSize);
        return shopMapper.selectShopWithUser(page);
    }

    //查询店铺,带评分
    public ShopScoreVO selectShopWithAvgScoreById(Integer id) {
        return shopMapper.selectShopWithAvgScoreById(id);
    }

    public Page<Shop> queryShopsByUserId(String userId, QueryPageDto pager) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Page<Shop> pageInfo = shopMapper.selectPage(new Page<Shop>(pager.getPageNum(), pager.getPageSize()), queryWrapper);
        System.out.println(pageInfo.getTotal());
        // 返回分页信息
        return pageInfo;
    }
}
